package cn.hj.pet.controller;

import cn.hj.basic.exception.BusinessException;
import cn.hj.basic.util.AjaxResult;
import cn.hj.basic.util.LoginContext;
import cn.hj.basic.util.PageList;
import cn.hj.pet.domain.Pet;
import cn.hj.pet.domain.SearchMasterMsg;
import cn.hj.pet.query.SearchMasterMsgQuery;
import cn.hj.pet.service.IPetService;
import cn.hj.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;



    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.findAll();
    }

    /**
     * 发布寻主信息
     * @param searchMasterMsg
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg,HttpServletRequest request){
        try {
            seachMasterMsgService.publish(searchMasterMsg, LoginContext.getCurrentLogininfo(request));
            return AjaxResult.newAjaxResult();
        } catch (BusinessException e){
            return AjaxResult.newAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.newAjaxResult().setMessage("系统错误，请稍后重试").setSuccess(false);
        }
    }

    /**
     * 用户查询自己的寻主消息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return seachMasterMsgService.user(query,LoginContext.getCurrentLogininfo(request));
    }

    /**
     * 查询待处理信息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        query.setState(0);//带处理信息
        return seachMasterMsgService.pending(query,LoginContext.getCurrentLogininfo(request));
    }

    /**
     * 查询已处理信息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        query.setState(1);//已处理信息
        return seachMasterMsgService.processed(query,LoginContext.getCurrentLogininfo(request));
    }

    /**
     * 处理信息
     * @param pet
     * @param request
     * @return
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            seachMasterMsgService.handle(pet, LoginContext.getCurrentLogininfo(request));
            return AjaxResult.newAjaxResult();
        } catch (BusinessException e){
            return AjaxResult.newAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.newAjaxResult().setMessage("系统错误，请稍后重试").setSuccess(false);
        }
    }
}
